//
//  PopupView.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/12.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "PopupView.h"
#import "PopViewTitleTableViewCell.h"

@interface PopupView ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
@property (nonatomic,copy) NSArray * titArr;
@end

@implementation PopupView
-(id)initPopupview:(NSArray *)popArr{
    if (self = [super initWithFrame:kAppWindow.bounds inSuperView:kAppWindow]) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];//145
        self.titArr = popArr;
        CGFloat tableHeight = popArr.count * kHeightProportion_In_iPhoneTriploid(44) + kHeightProportion_In_iPhoneTriploid(88);
        UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, KScreenHeight, KScreenWidth, tableHeight) style:UITableViewStylePlain];
        tableView.backgroundColor = [UIColor colorWithHexString:@"#FAFAFA"];
        tableView.showsVerticalScrollIndicator = NO;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.rowHeight = kHeightProportion_In_iPhoneTriploid(44);
        [self addSubview:tableView];
        
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:3 options:UIViewAnimationOptionCurveEaseIn animations:^{
            tableView.frame = CGRectMake(0, KScreenHeight - tableHeight, KScreenWidth, tableHeight);
        } completion:^(BOOL finished) {
            
        }];
        //
        UITapGestureRecognizer * tap_Header = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap_B_Action)];
        tap_Header.numberOfTouchesRequired = 1;
        tap_Header.numberOfTapsRequired = 1;
        tap_Header.delegate = self;
        [self addGestureRecognizer:tap_Header];
        self.userInteractionEnabled = YES;
    }
    return self;
}

-(void)tap_B_Action{
    [self remove_self];
}

-(void)remove_self{
    [self removeAllSubviews];
    [self removeFromSuperview];
}

- (void)removeAllSubviews {
    //[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    while (self.subviews.count) {
        [self.subviews.lastObject removeFromSuperview];
    }
}
#pragma mark -- UIGestureRecognizerDelegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return  YES;
}
#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.titArr.count;
    }else{
        return 1;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PopViewTitleTableViewCell * cell = [PopViewTitleTableViewCell cellWithTableView:tableView];
    if (indexPath.section == 0) {
        if (_haveTitle && indexPath.row == 0) {
            cell.titLabel.font = SYSTEMFONT(14);
            cell.titLabel.textColor = [UIColor colorWithHexString:@"#666666"];
        }
        cell.titLabel.text = self.titArr[indexPath.row];
    }else{
        cell.titLabel.text = @"取消";
    }
    [cell setSepartionLineWith:lineModel_All];
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(10))];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#EAEAEA"];
    if (section == 0) {
        headerView.height = 0;
    }
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }else{
        return kHeightProportion_In_iPhoneTriploid(10);
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (self.Block_SelectType) {
            self.Block_SelectType(indexPath.row);
        }
    }
    [self remove_self];
}

-(void)setHaveTitle:(BOOL)haveTitle{
    _haveTitle = haveTitle;
}

@end
